package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.entity.TeacherInfo;
import cn.stylefeng.guns.modular.school.entity.TripInfo;
import cn.stylefeng.guns.modular.school.entity.TripOrderInfo;
import cn.stylefeng.guns.modular.school.model.params.TripOrderInfoParam;
import cn.stylefeng.guns.modular.school.model.result.TripOrderInfoResult;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.modular.school.service.TripInfoService;
import cn.stylefeng.guns.modular.school.service.TripOrderInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/trip/order")
public class ApiNeedTokenTripOrderController extends BaseController {
    @Autowired
    private TripOrderInfoService tripOrderInfoService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private TripInfoService tripInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;

    @PostMapping(value = "pay")
    public Object pay(TripOrderInfoParam tripOrderInfoParam) {
        TripOrderInfo tripOrderInfo = tripOrderInfoService.getById(tripOrderInfoParam.getId());
        tripOrderInfo.setPayFlag("1");
        tripOrderInfoService.saveOrUpdate(tripOrderInfo);
        return SUCCESS_TIP;
    }

    @GetMapping(value = "detail")
    public Object detail(TripOrderInfoParam tripOrderInfoParam) {
        Map map = new HashMap();
        TripOrderInfo tripOrderInfo = tripOrderInfoService.getById(tripOrderInfoParam.getId());
        TripInfo tripInfo = tripInfoService.getById(tripOrderInfo.getTripId());
        SUCCESS_TIP.setData(map);
        map.put("tripOrderInfo", tripOrderInfo);
        map.put("tripInfo", tripInfo);
        return SUCCESS_TIP;
    }


    @GetMapping(value = "comment/list")
    public Object comment(TripOrderInfoParam tripOrderInfoParam) {
        Map map = new HashMap();
        List<TripOrderInfo> tripOrderInfos;
        List<TripOrderInfoResult> list = new ArrayList<>();
        String account = tripOrderInfoParam.getAccount();

        StudentInfo studentInfo = studentInfoService.getOne(new QueryWrapper<StudentInfo>().eq("CODE", account));
        // 学生的时候
        if (studentInfo != null) {
            tripOrderInfos = tripOrderInfoService.list(new QueryWrapper<TripOrderInfo>().eq("account", account).orderByDesc("order_date"));
        } else {
            // 教师
            TeacherInfo teacherInfo = teacherInfoService.getOne(new QueryWrapper<TeacherInfo>().eq("CODE", account));
            // 教师管的学生
            List<StudentInfo> students = studentInfoService.list(new QueryWrapper<StudentInfo>().eq("TEACHER_ID", teacherInfo.getId()));
            List<String> accounts = new ArrayList<>();
            students.forEach(studentInfo1 -> {
                accounts.add(studentInfo1.getCode());
            });
            // 学生的乘车信息
            tripOrderInfos = tripOrderInfoService.list(new QueryWrapper<TripOrderInfo>().in("account", accounts).orderByDesc("order_date"));
        }
        tripOrderInfos.forEach(tripOrderInfo -> {
            TripOrderInfoResult tripOrderInfoResult = new TripOrderInfoResult();
            list.add(tripOrderInfoResult);
            ToolUtil.copyProperties(tripOrderInfo, tripOrderInfoResult);
            TripInfo tripInfo = tripInfoService.getById(tripOrderInfo.getTripId());
            tripOrderInfoResult.setTripName(tripInfo.getName());
            tripOrderInfoResult.setPrice(tripInfo.getPrice());
            tripOrderInfoResult.setOrderDateStr(DateUtil.format(tripOrderInfo.getOrderDate(), "MM-dd HH:mm"));

            StudentInfo studentInfo1 = studentInfoService.getOne(new QueryWrapper<StudentInfo>().eq("CODE", tripOrderInfo.getAccount()));
            tripOrderInfoResult.setAccountName(studentInfo1.getName());
        });
        map.put("tripOrderInfos", list);
        map.put("studentInfo", studentInfo);


        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @GetMapping(value = "list")
    public Object list(TripOrderInfoParam tripOrderInfoParam) {
        Map map = new HashMap();
        List<TripOrderInfo> tripOrderInfos;
        List<TripOrderInfoResult> list = new ArrayList<>();
        String account = tripOrderInfoParam.getAccount();

        tripOrderInfos = tripOrderInfoService.list(new QueryWrapper<TripOrderInfo>().eq("account", account).orderByDesc("order_date"));
        tripOrderInfos.forEach(tripOrderInfo -> {
            TripOrderInfoResult tripOrderInfoResult = new TripOrderInfoResult();
            list.add(tripOrderInfoResult);
            ToolUtil.copyProperties(tripOrderInfo, tripOrderInfoResult);
            TripInfo tripInfo = tripInfoService.getById(tripOrderInfo.getTripId());
            tripOrderInfoResult.setTripName(tripInfo.getName());
            tripOrderInfoResult.setPrice(tripInfo.getPrice());
            tripOrderInfoResult.setOrderDateStr(DateUtil.format(tripOrderInfo.getOrderDate(), "MM-dd HH:mm"));
        });
        map.put("tripOrderInfos", list);
        // map.put("studentInfo", studentInfo);


        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @PostMapping(value = "update")
    public Object update(TripOrderInfoParam tripOrderInfoParam) {
        // 更新学生信用值
        TripOrderInfo tripOrderInfo = tripOrderInfoService.getById(tripOrderInfoParam.getId());
        StudentInfo studentInfo = studentInfoService.getOne(new QueryWrapper<StudentInfo>().eq("CODE", tripOrderInfo.getAccount()));
        studentInfo.setCredit(studentInfo.getCredit() + tripOrderInfoParam.getScore());
        studentInfoService.saveOrUpdate(studentInfo);

        tripOrderInfoService.update(tripOrderInfoParam);
        return SUCCESS_TIP;
    }

    /**
     * 绑定
     */
    @PostMapping(value = "add")
    public Object add(TripOrderInfoParam tripOrderInfoParam) {

        // 信用值check
        StudentInfo studentInfo = studentInfoService.getOne(new QueryWrapper<StudentInfo>().eq("CODE", tripOrderInfoParam.getAccount()));
        TripInfo tripInfo = tripInfoService.getById(tripOrderInfoParam.getTripId());
        if (studentInfo != null) {
            if (studentInfo.getCredit() < tripInfo.getNeedCreditNum()) {
                ErrorResponseData errorResponseData = new ErrorResponseData("信用过低不能乘车");
                return errorResponseData;
            }
        }

        String account = tripOrderInfoParam.getAccount();
        Date orderDate = tripOrderInfoParam.getOrderDate();
        Integer tripId = tripOrderInfoParam.getTripId();
        TripOrderInfo tripOrderInfo = tripOrderInfoService.getOne(new QueryWrapper<TripOrderInfo>().eq("account", account).eq("order_date", orderDate).eq("trip_id", tripId));
        if (tripOrderInfo != null) {
            ErrorResponseData errorResponseData = new ErrorResponseData("请勿重复预定");
            return errorResponseData;
        } else {
            tripOrderInfoService.add(tripOrderInfoParam);
        }


        return SUCCESS_TIP;
    }


}


